<template>
  <div class="app-container">

    <!-- 操作按钮区域 -->
    <div class="tool-bar-btn" style="width: 60%;">
      <el-input class="area-search-input" v-model="areaName" placeholder="请输入关键字" clearable />
      <el-button class="area-add-btn"  type="primary" icon="el-icon-plus" @click="handleAdd()" v-hasPerms="['mall:area:add']">新增</el-button>
      <el-button class="area-add-btn" style="margin-right: 8px;"  type="primary" icon="el-icon-search" @click="handleQuery">刷新</el-button>
    </div>

    <!-- 表格区域 -->
    <div :style="treeStyle" class="tree-list" >
      <el-tree :data="dataList" node-key="id" :filter-node-method="filterNode" ref="tree"
              :props="props" :expand-on-click-node="false" >
        <div class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ node.label }}</span>
          <span style="float: right;">
            <el-button type="text" icon="el-icon-plus" @click="handleAdd(data.id)" v-hasPerms="['mall:area:add']">添加下级</el-button>
            <el-button type="text" icon="el-icon-edit" @click="handleUpdate(data)" v-hasPerms="['mall:area:edit']">修改</el-button>
            <el-button type="text" icon="el-icon-delete" @click="handleDelete(data)" v-hasPerms="['mall:area:remove']">删除</el-button>
          </span>
        </div>
      </el-tree>
    </div>

    <!-- 添加或修改对话框 -->
    <EditArea ref="formDialog" @ok="getList" />

  </div>
</template>

<script>
import {httpGet, httpDel} from '@/api';
import { IndexListMixin } from '@/components/mixins/IndexListMixin';
import EditArea from './edit';
import { treeDataTranslate } from '@/utils/common'

export default {
  name: "Area",
  mixins:[IndexListMixin],
  components: {
    EditArea,
  },
  data() {
    return {
      props: {
        id: 'id',
        label: 'areaName',
        children: 'children'
      },
      areaName: null,
      treeStyle:{
        overflow: 'auto',
        height: '820px'
      },
      // 查询参数
      queryParams: {
        status: 1,
        pageNum: 1,
        pageSize: 10
      },
      url: {
        base: '/mall/area/',
      	list: '/mall/area/list'
      }
    };
  },
  created() {
    this.getList();
    window.addEventListener('resize', () => {
      this.refreshTreeHeight()
    })
  },
  mounted(){
    this.refreshTreeHeight()
  },
  watch: {
    areaName (val) {
      this.$refs.tree.filter(val)
    }
  },
  methods: {
    refreshTreeHeight () {
      this.$nextTick(() => {
        this.treeStyle.height = document.body.clientHeight - 170 + 'px'
      })
    },
    getList() {
      this.areaName = null;
      this.loading = true;
      this.$httpGet(this.url.list, this.queryParams).then(res=>{
          this.dataList = res.data.records;
          // this.dataList = treeDataTranslate(res.data.records, 'id', 'parentId');
      }).finally((res)=>{
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.selectedRows = [];
      this.selectedRowKeys = [];
      this.getList();
    },

    filterNode (value, data) {
      if (!value) return true
      return data.areaName.indexOf(value) !== -1
    },

    /** 新增按钮操作 */
    handleAdd(id) {
      this.$refs.formDialog.toAdd("添加地区", {parentId: id});
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.selectedRowKeys
      this.$refs.formDialog.toUpdate("修改地区", httpGet(this.url.base + id));
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.selectedRowKeys
      this.deleteData(this.url.base, ids);
    },

  }
};
</script>
<style scoped>
  .tree-list{
    width: 60%;
    overflow: auto;
    border: 1px black solid;
    border-radius: 8px;
  }
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
  .area-search-input {
    width: 30%;
  }
  .area-add-btn {
    float: right;
  }
</style>
